package com.northwest_man.gradle;

public class ModuleVersion {
    private int count;
    private String version;

    public ModuleVersion(final String version) {
        count = 1;
        this.version = version;
    }

    public void increaseCount() {
        this.count++;
    }

    public int getCount() {
        return count;
    }

    public String getVersion() {
        return version;
    }

    public void newVersionName(final String newVersionName) {
        final boolean shouldReplace = newVersionName(version, newVersionName);
        if (shouldReplace) {
            this.version = newVersionName;
        }
    }

    private boolean newVersionName(final String oldVersionName, final String newVersionName) {
        String[] oldVersionSplit = oldVersionName.split("\\.");
        String[] newVersionSplit = newVersionName.split("\\.");
        if (oldVersionSplit.length != newVersionSplit.length) {
            // 认为新版本为较大版本
            return true;
        }
        for (int i = 0; i < oldVersionSplit.length; i++) {
            if (Integer.parseInt(newVersionSplit[i]) > Integer.parseInt(oldVersionSplit[i])) {
                return true;
            } else if (Integer.parseInt(newVersionSplit[i]) < Integer.parseInt(oldVersionSplit[i])) {
                return false;
            }
        }
        return false;
    }
}
